<template>
  <div>
    <transition
      name="slide-fade" mode="out-in"
    >
      <custom-container v-if="!detailVisible">
        <custom-container-header no-spacing>
          <el-form :model="filters" ref="form" :disabled="loading"
                   inline size="medium"
                   @submit.native.prevent="getAll"
                   class="width-100"
          >
            <el-form-item label="查询时间:">
              <el-date-picker
                v-model="filters.date"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="{disabledDate: beforeToday.disabledDate}"
                value-format="timestamp"
                :default-time="['00:00:00', '23:59:59']"
              >
              </el-date-picker>
            </el-form-item>
            <!--<el-form-item>-->
            <!--<el-button type="primary" native-type="submit">查询</el-button>-->
            <!--</el-form-item>-->

            <el-form-item class="float-right">
              <custom-button-group v-model="time">
                <custom-button v-for="period in periods" :key="period.value" :label="period.value">
                  {{period.title}}
                </custom-button>
              </custom-button-group>
            </el-form-item>
          </el-form>
        </custom-container-header>
        <custom-container-body>
          <el-table :data="tableData.list" :loading="loading" element-loading-text="加载中..." border border-radius
                    :summary-method="getSummaries"
                    :local-summary-method="getLocalSummaries"
                    show-summary
                    show-local-summary
                    @sort-change="sortChange"
          >
            <el-table-column
              align="center"
              prop="date"
              label="日期">
            </el-table-column>
            <el-table-column
              align="center"
              prop="recharge"
              label="存款金额">
              <template slot-scope="{row}">
                <el-button type="link" size="small"
                           @click="openDetail({detailName: 'recharge-detail', props: {row, filters}})"
                >
                  {{row.recharge | currency}}
                </el-button>
              </template>
            </el-table-column>

            <el-table-column
              align="center"
              prop="withdraw"
              label="提款金额">
              <template slot-scope="{row}">
                <el-button type="link" size="small"
                           @click="openDetail({detailName: 'withdraw-detail', props: {row, filters}})"
                >
                  {{row.withdraw | currency}}
                </el-button>
              </template>
            </el-table-column>

            <el-table-column
              align="center"
              prop="rechargeUser"
              label="存款人数">
            </el-table-column>

            <el-table-column
              align="center"
              prop="withdrawUser"
              label="提款人数">
            </el-table-column>

            <template slot="empty">
              <div class="el-table__empty">
                <svg-icon class="el-table__empty-icon" icon-class="grid-empty-2"></svg-icon>
                <div class="el-table-text">暂无数据</div>
              </div>
            </template>
          </el-table>

          <div class="el-pagination-container">
            <el-pagination
              bordered
              @size-change="localGridSizeChange" @current-change="localGridCurrentChange"
              :current-page="filters.page" :page-size="filters.size"
              :total="tableData.total">
            </el-pagination>
          </div>
        </custom-container-body>
      </custom-container>
      <component
        :is="detailName"
        :info="detailProps"
        v-else
      ></component>
    </transition>
  </div>
</template>

<script type="text/jsx">

  import {rechargeWithdrawReportApi as getApi,} from '@/services/api/report';

  import {detailContent, grid, pickerOption} from '@/build';

  import WithdrawDetail from "./withdraw-detail";
  import RechargeDetail from "./recharge-detail";

  export default {
    name: "game-win-lose",

    mixins: [grid, pickerOption, detailContent],

    components: {
      RechargeDetail,
      WithdrawDetail
    },

    data() {
      return {
        filters: {
          date: undefined,
          page: 1,
          size: 10,
        },
        time: consts.THIS_WEEK,
        periods: _.filter(consts.TIME_PERIOD, (period) => {
          return _.indexOf([consts.THIS_WEEK, consts.LAST_WEEK, consts.THIS_MONTH, consts.LAST_MONTH], period.value) !== -1;
        }),
        tableData: {
          list: [],
          total: 0,
        },
        statistic: [
          {
            index: 1,
            prop: 'tableData.sum.recharge',
            format: (value) => {
              return <div>{_.currency(value)}</div>;
            }
          },
          {
            index: 2,
            prop: 'tableData.sum.withdraw',
            format: (value) => {
              return <div>{_.currency(value)}</div>;
            }
          },
          {
            index: 3,
            prop: 'tableData.sum.rechargeUser',
          },
          {
            index: 4,
            prop: 'tableData.sum.withdrawUser',
          },
        ]
      };
    },

    watch: {
      'filters.date'() {
        this.gridSearch();
      },
      'filters.page'() {
        this.doGet({action: 'getGrid'});
      }
    },

    mounted() {
      this.bindGet(getApi, 'tableData', {
        action: 'getGrid',
        sendingData: 'filters',
        format: data => {
          data.list = _(data.list).map((info, date) => {
            return {
              date,
              ...info
            };
          }).orderBy(['date'], ['desc']).value();
          // data.total = _.size(data.list);
          return data;
        }
      });
    }
  };

</script>

